<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      .tab {
        list-style: none;
      }

      .tab li {
        display: inline-block;
        border: 1px solid #000;
        background-color: aqua;
      }

      .tab li.active {
        background-color: gold;
      }

      img {
        width: 300px;
      }

      #app > div {
        display: none;
      }

      #app > .active {
        display: block;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <ul class="tab">
        <li
          v-for="(item,index) in fruits"
          :class="currentIndex==index? 'active' : ''"
          @click="changeIndex(index)"
        >
          {{item.title}}
        </li>
      </ul>

      <div
        v-for="(item,index) in fruits"
        :class="currentIndex==index? 'active' : ''"
      >
        <img :src="item.imgSrc" alt="" />
      </div>
      <input type="text" v-model.trim='title' placeholder="title">
      <input type="text" v-model.trim='imgSrc' placeholder="imgSrc">
      <button @click="addData">点击添加</button>
    </div>
    <script src="./vue.js"></script>
    <script>
      var vm = new Vue({
        el: "#app",
        data: {
          title: "",
          imgSrc: "",
          currentIndex: 0,
          fruits: [
            {
              title: "img1",
              imgSrc: "img/1.png",
            },
            {
              title: "img2",
              imgSrc: "img/2.png",
            },
            {
              title: "img3",
              imgSrc: "img/3.png",
            },
          ],
          flag: true,
        },
        methods: {
          changeIndex(i) {
            this.currentIndex = i;
          },
          addData() {
              if(!this.title) return alert("title shouldn't be empty")
              if(!this.imgSrc) return alert("imgSrc shouldn't be empty")
            let obj = {
              title: this.title,
              imgSrc: this.imgSrc,
            };

            // 一
            //   this.fruits.push(obj)
            //二
            this.$set(this.fruits, this.fruits.length, obj);
            //三
            // Vue.set(this.fruits,3,obj)
          },
        },
      });
    </script>
  </body>
</html>
